<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : btnType ? '详情' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="场次名称" prop="sessionName">
        <el-input
          :disabled="btnType==='detail'"
          v-model="dataForm.sessionName"
          placeholder="请输入场次名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="场次开始时间"
        prop="startTime"
      >
        <el-date-picker
          :disabled="btnType==='detail'"
          v-model="dataForm.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="场次结束时间"
        prop="endTime"
      >
        <el-date-picker
          :disabled="btnType==='detail'"
          v-model="dataForm.endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status" :disabled="btnType==='detail'">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" v-if="btnType !== 'detail'" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    // var validatePassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error('密码不能为空'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateComfirmPassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error('确认密码不能为空'))
    //   } else if (this.dataForm.password !== value) {
    //     callback(new Error('确认密码与密码输入不一致'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateEmail = (rule, value, callback) => {
    //   if (!isEmail(value)) {
    //     callback(new Error('邮箱格式错误'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateMobile = (rule, value, callback) => {
    //   if (!isMobile(value)) {
    //     callback(new Error('手机号格式错误'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateEndTime = (rule, value, callback) => {
    //   console.log(value, this.dataForm.startTime)
    //   // if (!isMobile(value)) {
    //   //   callback(new Error('手机号格式错误'))
    //   // } else {
    //   //   callback()
    //   // }
    // }
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        sessionName: '',
        startTime: '',
        endTime: '',
        status: 1
      },
      btnType: '',
      dataRule: {
        sessionName: [
          { required: true, message: '场次名称不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '场次开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          {
            required: true,
            message: '场次结束时间不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // watch: {
  //   dataForm(o, n) {
  //     console.log(o, n)
  //     this.endTime = o
  //   },
  // },
  methods: {
    init (id, type) {
      this.btnType = type
      this.dataForm.id = id || 0
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(
            `/coupon/coupon/session/info/${this.dataForm.id}`
          ),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.sessionName = data.session.sessionName
            this.dataForm.startTime = data.session.startTime
            this.dataForm.endTime = data.session.endTime
          }
        })
      }
      this.visible = true
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.dataForm)
          this.$http({
            url: this.$http.adornUrl(
              `/coupon/coupon/session/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              sessionName: this.dataForm.sessionName.replace(/-/g, ''),
              endTime: this.dataForm.endTime,
              startTime: this.dataForm.startTime,
              status: this.dataForm.id ? this.dataForm.status : undefined
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
